package cn.sciento.farm.devicemanager.api.jsonrpc.impl;


import cn.sciento.farm.devicemanager.api.jsonrpc.TimerJsonApi;
import cn.sciento.farm.devicemanager.common.constant.ResultConstant;
import cn.sciento.farm.devicemanager.common.util.DateUtil;
import cn.sciento.farm.devicemanager.common.util.ReturnUtil;
import cn.sciento.farm.devicemanager.dao.entity.DevTimer;
import cn.sciento.farm.devicemanager.dao.entity.DevTimerLog;
import cn.sciento.farm.devicemanager.dao.model.PageConditionQuery;
import cn.sciento.farm.devicemanager.dao.model.ResponseMessage;
import cn.sciento.farm.devicemanager.service.TimerLogService;
import cn.sciento.farm.devicemanager.service.TimerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@AutoJsonRpcServiceImpl
public class TimerJsonApiimpl implements TimerJsonApi {


    @Autowired
    private TimerService timerService;

    @Autowired
    private TimerLogService timerLogService;

    @Override
    public ResponseMessage create(DevTimer timer) {
        if(timer.getControllerId() <=0
                || StringUtils.isEmpty(timer.getCirculation())
                || timer.getStartTime() < 0){
            return  ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }else {
            timer.setCreateTime(System.currentTimeMillis());
            timer.setUpdateTime(System.currentTimeMillis());
            int count = timerService.insert(timer);
            return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,count);
        }
    }

    @Override
    public ResponseMessage update(DevTimer timer) {
        if (timer.getId()<=0){
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }else {
            DevTimer devTimer = timerService.getById(timer.getId());
            devTimer.setDuration(timer.getDuration());
            devTimer.setCirculation(timer.getCirculation());
            devTimer.setStartTime(timer.getStartTime());
            devTimer.setStatus(timer.getStatus());
            int count = timerService.updateById(devTimer);
            return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,count);
        }
    }

    @Override
    public ResponseMessage delete(Long id) {
        int count = timerService.deleteById(id);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,count);
    }

    @Override
    public ResponseMessage list(Long controllerId) {
        List<DevTimer> devTimerList = timerService.getByControllerId(controllerId);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,devTimerList);
    }

    @Override
    public ResponseMessage get(Long id) {
        DevTimer devTimer = timerService.getById(id);
        if(devTimer == null){
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }else {
            return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,devTimer);
        }
    }

    @Override
    public ResponseMessage getTimerLog(PageConditionQuery data) {
        if (data == null || data.getAreaId() == null
                || data.getAreaId() < 0
                || data.getControllerId() < 0
                || data.getCurrentPage() < 1
                || data.getPageSize() < 1
        ){
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        if (data.getCurrentPage() == null ){
            data.setCurrentPage(1);
        }
        if (data.getPageSize()== null){
            data.setPageSize(10);
        }
        if (data.getBeginTime() == null){
            Date date = DateUtil.getTodayZeroHour();
            data.setBeginTime(date.getTime());
        }
        if ( data.getEndTime() == null) {
            Date date = DateUtil.getTodayLastHour();
            data.setEndTime(date.getTime());
        }
        IPage<DevTimerLog> timerLogs = timerLogService.getTimerLog(data);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,timerLogs);
    }
}
